//
//  QRCodeContentView.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/12/11.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "QRCodeContentView.h"
#import "QRCodeManager.h"
@interface QRCodeContentView ()
@property (weak, nonatomic) IBOutlet UILabel *qrCodeTitleLb;
@property (weak, nonatomic) IBOutlet UIButton *qrCodeChangeBtn;
@property (weak, nonatomic) IBOutlet UIView *qrCodeBackView;
@property (weak, nonatomic) IBOutlet UILabel *qrCodeDesLb;
@property (weak, nonatomic) IBOutlet UIView *dashLineView;
@property (nonatomic,strong) UIImageView  *qrCodeIV;
@end

@implementation QRCodeContentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init{
    if (self = [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = UIColorWithRGBA(187, 187, 187, 1).CGColor;
        self.layer.cornerRadius = 8.0;
        self.layer.masksToBounds = YES;
        [self addUI];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    [self drawDashLine:_dashLineView lineLength:10 lineSpacing:3 lineColor:UIColorWithRGBA(187, 187, 187, 1)];
}

- (void)addUI{
    _qrCodeIV = [UIImageView new];
     [_qrCodeBackView addSubview:_qrCodeIV];
    [_qrCodeIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_qrCodeBackView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [_qrCodeChangeBtn addTarget:self action:@selector(qrCodeChangeBtnMethod:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setQrCodeInfo:(NSString *)qrCodeInfo{
    _qrCodeInfo = qrCodeInfo;
    _qrCodeIV.image = [QRCodeManager getQRCodeWithInfo:qrCodeInfo];
}

- (void)qrCodeChangeBtnMethod:(UIButton *)sender{
    CABasicAnimation*rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    if (sender.selected) {
        rotationAnimation.fromValue = [NSNumber numberWithFloat: M_PI];
        rotationAnimation.toValue = [NSNumber numberWithFloat: 0.0];
    }else{
        rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
    }
    rotationAnimation.duration = 0.4;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [sender.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    NSLog(@"旋转完毕");
    sender.selected = !sender.selected;
}

- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    NSLog(@"%@",NSStringFromCGRect(lineView.frame));
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线高度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

- (void)dealloc{
    NSLog(@"释放QRCodeView了");
}

@end
